package cn.shengongying.admin.controller;

import cn.shengongying.admin.dto.CircleListInDTO;
import cn.shengongying.bean.DataTablePageResult;
import cn.shengongying.bean.ResponseResult;
import cn.shengongying.dubbo.modal.CircleList;
import cn.shengongying.dubbo.service.ICircleListService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * author: RXK
 * date: 2022/2/19 星期六
 * desc:
 */
@Controller
@RequestMapping("/sys/circle/list")
public class CircleListController extends BaseController{


    @DubboReference(version = ICircleListService.version)
    private ICircleListService circleListService;


    @RequestMapping("/index")
    public String index() {
        return "/circle/index";
    }

    @ResponseBody
    @RequestMapping("/list")
    public DataTablePageResult<List<CircleList>> list(@RequestBody CircleListInDTO inDTO) {
        List<CircleList> list = circleListService.list();
        inDTO.setData(list);
        long count = circleListService.count();
        inDTO.setRecordsTotal(count);
        return inDTO;
    }


    @ResponseBody
    @RequestMapping("/detail")
    public ResponseResult<CircleList> detail(@RequestBody CircleListInDTO inDTO) {
        CircleList bannerList = circleListService.getById(inDTO.getId());
        return ResponseResult.success(bannerList);
    }

    @ResponseBody
    @RequestMapping("/save")
    public ResponseResult<String> save(CircleListInDTO inDTO, HttpServletRequest request) {
        CircleList circleList = inDTO.bannerList();
        String file;
        try {
            file = multiFile(request);
        } catch (Exception e) {
            logger.error("上传文件出现异常",e);
            return ResponseResult.fail();
        }
        circleList.setImg(file);
        circleList.setUserId(0L);
        circleListService.saveOrUpdate(circleList);
        return ResponseResult.success();
    }

    @ResponseBody
    @RequestMapping("/update/status")
    public ResponseResult<String> updateStatus(@RequestBody CircleListInDTO inDTO) {
        CircleList circleList = new CircleList();
        circleList.setId(inDTO.getId());
        circleList.setCircleStatus(inDTO.getStatus());
        circleListService.updateById(circleList);
        return ResponseResult.success();
    }

    @ResponseBody
    @RequestMapping("/delete")
    public ResponseResult<String> delete(@RequestBody CircleListInDTO inDTO) {
        circleListService.removeById(inDTO.getId());
        return ResponseResult.success();
    }




}
